package mylittleinvestigations.DialogCutscenes;

import java.io.IOException;
import java.util.HashMap;
import mylittleinvestigations.CaseInformation.Case;
import mylittleinvestigations.CaseInformation.CaseXmlReader;
import mylittleinvestigations.CaseInformation.CaseXmlWriter;
import mylittleinvestigations.Vector2;
import org.newdawn.slick.Color;

public class DialogCutsceneAnimationFrame
{
    private int msDuration;
    private String newBackgroundTextureId;
    private HashMap<String, Vector2> positionChangeHashMap;
    private HashMap<String, Color> tintChangeHashMap;

    public DialogCutsceneAnimationFrame(int msDuration)
    {
        this.msDuration = msDuration;
        this.positionChangeHashMap = new HashMap<String, Vector2>();
        this.tintChangeHashMap = new HashMap<String, Color>();
    }
    
    public int getMsDuration() { return this.msDuration; }
    public String getNewBackgroundTextureId() { return this.newBackgroundTextureId; }

    public void AddPositionChange(String elementId, int newXPosition, int newYPosition)
    {
        this.positionChangeHashMap.put(elementId, new Vector2(newXPosition, newYPosition));
    }

    public void AddTintChange(String elementId, int newATint, int newRTint, int newGTint, int newBTint)
    {
        this.tintChangeHashMap.put(elementId, new Color(newRTint, newGTint, newBTint, newATint));
    }
    
    public void AddBackgroundChange(String newBackgroundTextureId)
    {
        this.newBackgroundTextureId = newBackgroundTextureId;
    }

    public void Begin()
    {
        String[] positionChangeIds = this.positionChangeHashMap.keySet().toArray(new String[0]);
        
        for (int i = 0; i < positionChangeIds.length; i++)
        {
            DialogCutsceneAnimationElement element = Case.getInstance().getDialogCutsceneManager().GetElementFromId(positionChangeIds[i]);
            Vector2 newPosition = this.positionChangeHashMap.get(positionChangeIds[i]);

            if (element != null)
            {
                element.SetTargetPosition((int)newPosition.getX(), (int)newPosition.getY(), this.msDuration);
            }
        }
        
        String[] tintChangeIds = this.tintChangeHashMap.keySet().toArray(new String[0]);

        for (int i = 0; i < tintChangeIds.length; i++)
        {
            DialogCutsceneAnimationElement element = Case.getInstance().getDialogCutsceneManager().GetElementFromId(tintChangeIds[i]);
            Color newTint = this.tintChangeHashMap.get(tintChangeIds[i]);

            if (element != null)
            {
                element.SetTargetTint((int)(255 * newTint.a), (int)(255 * newTint.r), (int)(255 * newTint.g), (int)(255 * newTint.b), this.msDuration);
            }
        }
    }

    void SaveAsXml(CaseXmlWriter writer) throws IOException
    {
        writer.StartElement("DialogCutsceneAnimationFrame");
        writer.WriteIntElement("MsDuration", this.msDuration);
        
        writer.StartElement("PositionChangeHashMap");
        String[] ids = this.positionChangeHashMap.keySet().toArray(new String[0]);
        
        for (int i = 0; i < ids.length; i++)
        {
            writer.StartElement("Entry");
            String id = ids[i];
            writer.WriteTextElement("Id", id);
            
            writer.StartElement("PositionChange");
            this.positionChangeHashMap.get(id).SaveToXml(writer);
            writer.EndElement();
            
            writer.EndElement();
        }
        
        writer.EndElement();
        
        writer.StartElement("TintChangeHashMap");
        ids = this.tintChangeHashMap.keySet().toArray(new String[0]);
        
        for (int i = 0; i < ids.length; i++)
        {
            writer.StartElement("Entry");
            String id = ids[i];
            writer.WriteTextElement("Id", id);
            
            writer.StartElement("TintChange");
            writer.WriteColorElement(this.tintChangeHashMap.get(id));
            writer.EndElement();
            
            writer.EndElement();
        }
        
        writer.EndElement();
        
        if (this.newBackgroundTextureId != null)
        {
            writer.WriteSpriteId("NewBackgroundSpriteId", this.newBackgroundTextureId);
        }
        
        writer.EndElement();
    }

    public DialogCutsceneAnimationFrame(CaseXmlReader reader)
    {
        this.positionChangeHashMap = new HashMap<String, Vector2>();
        this.tintChangeHashMap = new HashMap<String, Color>();
        
        reader.StartElement("DialogCutsceneAnimationFrame");
        this.msDuration = reader.ReadIntElement("MsDuration");
        
        reader.StartElement("PositionChangeHashMap");
        reader.StartList("Entry");
        
        while (reader.MoveToNextListItem())
        {
            String id = reader.ReadTextElement("Id");
            
            reader.StartElement("PositionChange");
            Vector2 positionChange = new Vector2(reader);
            reader.EndElement();
            
            this.positionChangeHashMap.put(id, positionChange);
        }
        
        reader.EndElement();
        
        reader.StartElement("TintChangeHashMap");
        reader.StartList("Entry");
        
        while (reader.MoveToNextListItem())
        {
            String id = reader.ReadTextElement("Id");
            
            reader.StartElement("TintChange");
            Color tintChange = reader.ReadColorElement();
            reader.EndElement();
            
            this.tintChangeHashMap.put(id, tintChange);
        }
        
        reader.EndElement();
        
        if (reader.ElementExists("NewBackgroundSpriteId"))
        {
            this.newBackgroundTextureId = reader.ReadTextElement("NewBackgroundSpriteId");
        }
        
        reader.EndElement();
    }

    void markChildSprites(String markParent)
    {
        if (this.newBackgroundTextureId != null)
        {
            Case.getInstance().getSpriteManager().GetSpriteFromId(this.newBackgroundTextureId).markChildSprites(markParent);
        }
    }
}